<!DOCTYPE HTML>
<html>

<head>
    <title>proton.js-behaviour-firefilies</title>
    <meta name="viewport" id="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta charset="utf-8">
    <style type="text/css">
        body {
            background-color: #111;
            margin: 0px;
            overflow: hidden;
            -moz-user-select: none;
            -webkit-user-select: none;
            -ms-user-select: none;
            -khtml-user-select: none;
            user-select: none;
        }
        
        #container {
            width: 1003px;
            height: 610px;
            margin-top: 50px;
            margin-left: -501px;
            left: 50%;
            position: absolute;
        }
        
        #testCanvas {
            background: #000;
        }
        .shadow {
            box-shadow: 0 0 20px #000;
        }
    </style>
</head>

<body>
    <div id="container" class="shadow">
        <canvas id="testCanvas"></canvas>
    </div>
    <script src="../../lib/requestAnimationFrame.min.js"></script>
    <script src="../../lib/stats.min.js"></script>
    <script src="//localhost:3001/build/proton.js"></script>
    <script>
        var canvas;
        var context;
        var proton;
        var renderer;
        var emitter;
        var stats;
        var mouseObj;
        var bgImg;
        var repulsionBehaviour, crossZoneBehaviour;

        Main();

        function Main() {
            canvas = document.getElementById("testCanvas");
            canvas.width = 1003;
            canvas.height = 610;
            context = canvas.getContext('2d');
            addStats();
            loadImage();
        }

        function beginScene() {
            createProton();
            createRenderer();
            tick();
            canvas.addEventListener('mousemove', mousemoveHandler, false);
        }

        function loadImage() {
            var image = new Image()
            image.onload = function(e) {
                bgImg = e.target;
                beginScene();
            }

            image.src = 'image/bg.jpg';
        }

        function addStats() {
            stats = new Stats();
            stats.setMode(2);
            stats.domElement.style.position = 'absolute';
            stats.domElement.style.left = '0px';
            stats.domElement.style.top = '0px';
            //document.getElementById('container').appendChild(stats.domElement);
        }

        function createProton() {
            proton = new Proton;
            emitter = new Proton.Emitter();

            emitter.damping = 0.0075;
            emitter.rate = new Proton.Rate(180);
            
            emitter.addInitialize(new Proton.Body('image/particle.png', 32));
            emitter.addInitialize(new Proton.Position(new Proton.RectZone(0, 0, 1003, 550)));
            emitter.addInitialize(new Proton.Mass(1), new Proton.Radius(Proton.getSpan(5, 10)));
            
            mouseObj = {
                x: canvas.width / 2,
                y: canvas.height / 2
            };
            repulsionBehaviour = new Proton.Repulsion(mouseObj, 0, 0);
            crossZoneBehaviour = new Proton.CrossZone(new Proton.RectZone(-2, 0, 1005, 550), 'cross');
            emitter.addBehaviour(repulsionBehaviour, crossZoneBehaviour);
            emitter.addBehaviour(new Proton.Scale(Proton.getSpan(.1, .4)));
            emitter.addBehaviour(new Proton.Alpha(.5));
            emitter.addBehaviour(new Proton.RandomDrift(10, 10, .2));
            
            emitter.addBehaviour({
                initialize: function(particle) {
                    particle.tha = Math.random() * Math.PI;
                    particle.thaSpeed = 0.015 * Math.random() + 0.005;
                },

                applyBehaviour: function(particle) {
                    particle.tha += particle.thaSpeed;
                    particle.alpha = Math.abs(Math.cos(particle.tha));
                }
            });
            
            emitter.emit('once');
            proton.addEmitter(emitter);
        }

        function mousemoveHandler(e) {
            if (e.layerX || e.layerX == 0) {
                mouseObj.x = e.layerX;
                mouseObj.y = e.layerY;
            } else if (e.offsetX || e.offsetX == 0) {
                mouseObj.x = e.offsetX;
                mouseObj.y = e.offsetY;
            }

            repulsionBehaviour.reset(mouseObj, 5, 100);
        }

        function createRenderer() {
            renderer = new Proton.CanvasRenderer(canvas);
            renderer.onProtonUpdate = function() {
                if (bgImg)
                    context.drawImage(bgImg, 0, 0);
            };
            proton.addRenderer(renderer);
        }

        function tick() {
            requestAnimationFrame(tick);

            stats.begin();
            proton.update();
            stats.end();
        }
    </script>
</body>

</html>